package com.tuoyu.peopletest.media;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.SoundPool;

import com.tuoyu.peopletest.utils.SCLogUtils;


/**
 * 媒体声音
 * 特效音
 * 根据项目需求SoundPool 使用一个销毁一个, 不用保持,
 *
 */
public class MediaSoundUtils {
    private final static String TAG = "MediaSoundUtils";
    private SoundPool soundPool;
    private static MediaSoundUtils sInstance;
    private Context context;
    //private HashMap<Integer, Integer> soundMap = new HashMap<>();
    private int mSoundId;
    private int mSoundId02;
    private AudioAttributes attributes;

    private MediaSoundUtils() {

    }
    public static MediaSoundUtils get() {
        return new MediaSoundUtils();
    }

    public MediaSoundUtils initSoundPool(Context context) {
        this.context = context;
        attributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_GAME)  // 设置音效使用的场景
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) // 设置音效的类型
                .build();

        if (attributes == null) {
            throw new IllegalArgumentException("MediaSoundUtils Invalid null AudioAttributes");
        }

        soundPool = new SoundPool.Builder()
                .setAudioAttributes(attributes)  // 设置音效池的属性
                .setMaxStreams(5)  // 设置最大可以容纳10个音频流
                .build();

        return this;
    }


    /**
     * 设置声音
     */
    public MediaSoundUtils setSoundRes(int resId){
        try {
            mSoundId = soundPool.load(context, resId, 1);
            return this;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    public MediaSoundUtils setSoundRes02(int resId){
        try {
            mSoundId02 = soundPool.load(context, resId, 1);
            return this;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 播放
     */
    public void play() {
        soundPool.play(mSoundId, 1, 1, 10, 0, 1);
    }

    public void stop(){
        soundPool.stop(mSoundId);
    }

    public void play02() {
        soundPool.play(mSoundId02, 1, 1, 10, 0, 1);
    }

    public void stop02(){
        soundPool.stop(mSoundId02);
    }

    public void play(int resId) {
        mSoundId = soundPool.load(context, resId, 1);
        setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                SCLogUtils.e(TAG, "onLoadComplete=" + sampleId);
                soundPool.play(mSoundId, 1, 1, 10, 0, 1);
            }
        });
    }

    public void release() {
        if(soundPool==null){
            return;
        }
        soundPool.autoPause();
        soundPool.unload(mSoundId);
        soundPool.release();
        soundPool =null;
    }



    /**
     * 加载监听
     *
     * @param listener
     */
    public void setOnLoadCompleteListener(SoundPool.OnLoadCompleteListener listener) {
        soundPool.setOnLoadCompleteListener(listener);
    }
}
